package com.ecube.test;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author zhuweijun
 * @version 1.0.0
 * @since 2025/8/5 16:18
 */
public class SmoothTransition {

    private static double currentValue; // 当前值
    private final static double targetValue; // 目标值
    private static int steps; // 步数（10分钟内每隔10秒执行一次，共60步）

    static {
        currentValue = 0; // 示例初始值
        targetValue = 100; // 示例目标值
        steps = 60; // 10分钟内每隔10秒执行一次，总共60次
    }

    public static void main(String[] args) {
        startSmoothTransition(currentValue, targetValue, steps);
    }

    /**
     * 开始平滑过渡
     *
     * @param startValue 初始值
     * @param endValue   目标值
     * @param totalSteps 总步数
     */
    public static void startSmoothTransition(double startValue, double endValue, int totalSteps) {
        currentValue = startValue;
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            int currentStep = 0;
            double stepSize = (endValue - startValue) / totalSteps; // 每一步的增量

            @Override
            public void run() {
                if (currentStep < totalSteps) {
                    currentValue += stepSize;
                    System.out.println("Step " + (currentStep + 1) + ": Current Value = " + currentValue);
                    currentStep++;
                } else {
                    this.cancel(); // 结束定时任务
                }
            }
        };

        // 每隔10秒执行一次任务
        timer.schedule(task, 0, 10000);
    }
}
